package kr.moiba.backoffice.result.web;

import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import kr.moiba.user.service.UserSvc;
import kr.moiba.vertification.domain.Vertification;
import kr.moiba.vertification.service.VertificationSvc;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 이 영호(Youngho Lee)
 */

@Controller("backofficeResult")
@RequestMapping("/backoffice/result")
public class ResultCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private MessageSourceAccessor messages;
	
	@Autowired
	private VertificationSvc vertificationSvc;

	@Autowired
	private UserSvc userSvc;
	
	@RequestMapping(value={"/list.do"})
	public Map<String, Object> list(HttpSession session) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		Map<String, Object> condition = new HashMap<String, Object>();
		resultMap.put("result", vertificationSvc.findbyListResult(condition));
		return resultMap;
	}
	
	@RequestMapping(value={"/listDetail.do"})
	public Map<String, Object> listDetail(HttpServletRequest request, HttpSession session, @RequestParam(required=true, value="type") String type) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		Map<String, Object> condition = new HashMap<String, Object>();
		if(type.equals("pay")){
			condition.put("type", 2);	
		}else{
			condition.put("type", 1);
		}

		//페이징 처리 parameter		
		int maxnum = ServletRequestUtils.getIntParameter(request, "maxnum", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		condition.put("maxnum", maxnum);
		condition.put("page", (page-1)*maxnum);
		
		resultMap.putAll(vertificationSvc.findbyListPaging(condition));
		resultMap.put("maxnum", maxnum);
		resultMap.put("page", page);
		
		return resultMap;
	}
	
	@RequestMapping(value={"/detail.do"})
	public Map<String, Object> detail(HttpServletRequest request, HttpSession session, @RequestParam(required=true, value="idx") Integer idx, @RequestParam(required=false, value="fAct") String fAct, Vertification vertification) throws Exception {
		//검증 결과 등록
		if(fAct != null && fAct.equals("modify")){
			//검증 결과 파일 업로드
			MultipartFile testResultFile = vertification.getResultFile();
			if(testResultFile != null && ! testResultFile.isEmpty()){
				long time = Calendar.getInstance().getTimeInMillis();
				String prefixNm = "testResultfile_" + Long.toString(time);
				String fileNm = prefixNm +"."+(testResultFile.getOriginalFilename()).split("\\.")[1];
				
				//업로드 경로
				String uploadDir = messages.getMessage("file.testResultFile.uploadDir");
				
				//viewUrl
				String viewUrl = messages.getMessage("file.testResultFile.viewUrl");

				File file = new File(uploadDir+fileNm);
				String url = viewUrl+fileNm;
				vertification.setTestResultFile(url);

				if(! file.isDirectory()){	//해당 폴더가 없을 경우 폴더 생성
					new File(uploadDir).mkdirs();
				}
				
				testResultFile.transferTo(file);
			}
			
			vertificationSvc.modify(vertification);
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
				
		resultMap.put("vertification", vertificationSvc.findby(idx));
		
		//history
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("vIdx", idx);
		resultMap.put("vertificationHistory", vertificationSvc.findbyListHistory(condition));
		
		//이용자 목록
		condition.put("authority", "ROLE_USER");
		condition.put("enabled", "1");
		condition.put("isDel", "0");
		resultMap.put("userList", userSvc.findbyList(condition));
		
		return resultMap;
	}

}